﻿using System.ComponentModel.Composition;
using System.Linq;
using System.Web.Mvc;
using RecordTime.Infrastructure.Mapping;
using RecordTime.Infrastructure.Services;
using RecordTime.ProjectManagement.ViewModels;
using Project = RecordTime.Models.Project;
using Task = RecordTime.Models.Task;

namespace RecordTime.Portal.Controllers
{
    public class TaskController : Controller
    {
        [Import]
        public IMappingService MappingService { get; set; }

        [Import]
        public IProjectManagementService ProjectManagementService { get; set; }

        //
        // GET: /Task/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View(new TaskViewModel());
        }

        [HttpPost]
        public ActionResult Create(TaskViewModel taskViewModel)
        {
            var task = MappingService.Get(taskViewModel);
            ProjectManagementService.CreateTask(task);
            return View("Index");
        }

        public JsonResult GetTasks()
        {
            var query = from task in ProjectManagementService.GetTasks()
                        select new { task.TaskId, task.TaskName, task.Project.ProjectName };
            return Json(query, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Edit(int id)
        {
            var task = new Task()
                           {
                               TaskId = id,
                               TaskName = "My Task",
                               Project = new Project() { ProjectId = 2, ProjectName = "Harvester" }
                           };
            var taskViewModel = MappingService.Get(task);

            return View(taskViewModel);
        }
    }
}
